﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	LookupValue.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Diagnostics;

namespace Smart.Common.Entities
{
	/// <summary>
	/// Lookup value
	/// </summary>
	[Serializable]
	[DebuggerStepThrough]
	public class LookupValue
	{
		/// <summary>
		/// Lookup ID
		/// </summary>
		public Identifier ID { get; set; }

		/// <summary>
		/// Lookup Title
		/// </summary>
		public string Title { get; set; }

		/// <summary>
		/// Lookup value
		/// </summary>
		public LookupValue(Identifier id)
		{
			ID = id.IsEmpty ? Identifier.Empty : id;
			Title = null;
		}

		/// <summary>
		/// Lookup value
		/// </summary>
		public LookupValue(Identifier id, string title)
		{
			if (id.IsEmpty)
			{
				ID = Identifier.Empty;
				Title = null;
			}
			else
			{
				ID = id;
				Title = title;
			}
		}

		/// <summary>
		/// Checks whether this value is empty
		/// </summary>
		public bool IsEmpty
		{
			get { return ID.IsEmpty; }
		}

		/// <summary>
		/// Set new lookup value
		/// </summary>
		public void Set(Identifier id)
		{
			ID = id.IsEmpty ? Identifier.Empty : id;
			Title = null;
		}

		/// <summary>
		/// Set new lookup value
		/// </summary>
		public void Set(Identifier id, string title)
		{
			if (id.IsEmpty)
			{
				ID = Identifier.Empty;
				Title = null;
			}
			else
			{
				ID = id;
				Title = title;
			}
		}

		/// <summary>
		/// Format in "id;#title"
		/// </summary>
		public override string ToString()
		{
			return (IsEmpty ? null : SPH.FormatLookupValue(ID.Int, Title));
		}

		/// <summary>
		/// Compare lookup values only by ID property
		/// </summary>
		public bool Equals(LookupValue obj)
		{
			return ID.Equals(obj.ID);
		}

		/// <summary>
		/// Compare lookup values only by ID property
		/// </summary>
		public override bool Equals(object obj)
		{
			if (obj == null || GetType() != obj.GetType())
			{
				return false;
			}

			return
				Equals((LookupValue)obj);
		}

		/// <summary>
		/// Hash code using ID property
		/// </summary>
		public override int GetHashCode()
		{
			return ID.GetHashCode();
		}

		public static LookupValue ParseInt(string value)
		{
			if (value.IsNullOrEmpty())
				return new LookupValue(Identifier.Empty);

			int id;
			string title;

			SPH.DispatchLookupValue(value, out id, out title);

			return
				id <= 0
					? new LookupValue(Identifier.Empty)
					: new LookupValue(id, title);
		}

		public static LookupValue FromEntity(Entity entity)
		{
			return
				entity == null
					? new LookupValue(Identifier.Empty)
					: new LookupValue(entity.ID, entity.Title);
		}

		public static bool operator ==(LookupValue lv1, LookupValue lv2)
		{
			if ((object)lv1 == null && (object)lv2 == null)
				return true;

			if ((object)lv1 == null || (object)lv2 == null)
				return false;

			return
				lv1.Equals(lv2);
		}

		public static bool operator !=(LookupValue lv1, LookupValue lv2)
		{
			if ((object)lv1 == null && (object)lv2 == null)
				return false;

			if ((object)lv1 == null || (object)lv2 == null)
				return true;

			return
				!lv1.Equals(lv2);
		}

		public static bool operator ==(LookupValue lv1, int lv2)
		{
			if ((object)lv1 == null)
				return false;

			return
				lv1.ID.Int == lv2;
		}

		public static bool operator !=(LookupValue lv1, int lv2)
		{
			if ((object)lv1 == null)
				return true;

			return
				lv1.ID.Int != lv2;
		}
	}
}